package com.living.filedownloader.db;

import android.text.TextUtils;

import com.liulishuo.filedownloader.FileDownloader;
import com.liulishuo.filedownloader.model.FileDownloadStatus;
import com.living.filedownloader.bean.TasksManagerModel;
import com.md.basedpc.persistence.PHDbUtils;
import com.md.network.api.Audio;

import java.util.ArrayList;
import java.util.List;

public class DownLoadDBManger {

    private static DownLoadDBManger instance = null;

    public static DownLoadDBManger getInstance() {
        if (instance == null) instance = new DownLoadDBManger();
        return instance;
    }

    /***
     * 批量保存 音频
     * @param albumDB
     * @param audioDB
     */
    public static void saveAudioAblum(AlbumDB albumDB, AudioDB audioDB) {
        saveAlbum(albumDB);
        saveAudio(audioDB.getAlbum_id(), audioDB);
    }

    /***
     * 批量保存 音频
     *
     * @param albumDB
     * @param audioList
     */
    public static void saveAudioList(AlbumDB albumDB, List<AudioDB> audioList) {
        saveAlbum(albumDB);
        for (AudioDB audioDB : audioList) {
            saveAudio(audioDB.getAlbum_id(), audioDB);
        }
    }

    /**
     * 保存 专辑信息
     *
     * @param albumDB
     * @return
     */
    private static AlbumDB saveAlbum(AlbumDB albumDB) {
        String where = " 1=1 ";
        if (!TextUtils.isEmpty(albumDB.getId()))
            where += " and id=" + albumDB.getId() + "";
        try {
            List<AlbumDB> dbs = PHDbUtils.getInstance().query(AlbumDB.class, where);
            if (dbs == null || dbs.size() == 0) {
                PHDbUtils.getInstance().insert(albumDB);
            } else {
                return dbs.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 保存 音频信息
     *
     * @param audioDB
     */
    private static AudioDB saveAudio(String albumId, AudioDB audioDB) {
        audioDB.setAlbum_id(albumId);
        String where = " 1=1 ";
        if (!TextUtils.isEmpty(audioDB.getId()))
            where += " and id=" + audioDB.getId() + "";
        if (!TextUtils.isEmpty(albumId))
            where += " and album_id=" + albumId + "";
        try {
            List<AudioDB> dbs = PHDbUtils.getInstance().query(AudioDB.class, where);
            if (dbs == null || dbs.size() == 0) {
                PHDbUtils.getInstance().insert(audioDB);
            } else {
                return dbs.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除 音频信息
     *
     * @param audioId
     * @return
     */
    public static boolean deleteAudio(String albumId, String audioId) {
        boolean isDelSucess = false;
        String where = " 1=1 ";
        if (!TextUtils.isEmpty(albumId))
            where += " and album_id=" + albumId + "";
        if (!TextUtils.isEmpty(audioId))
            where += " and id=" + audioId + "";
        try {
            isDelSucess = PHDbUtils.getInstance().delete(AudioDB.class, where);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isDelSucess;
    }

    /**
     * 判断 是否下载
     *
     * @return
     */
    public boolean isAudioExite(String albumId, String audioId) {
        boolean isExist = false;
        String where = " 1=1 ";
        if (!TextUtils.isEmpty(albumId))
            where += " and album_id=" + albumId + "";
        if (!TextUtils.isEmpty(audioId))
            where += " and id=" + audioId + "";
        try {
            List<AudioDB> dbs = PHDbUtils.getInstance().query(AudioDB.class, where);
            if (dbs != null && dbs.size() > 0) {
                isExist = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isExist;
    }

    /**
     * 检验是否删除
     *
     * @return
     */
    public boolean checkDeleteAudioId(String audioId) {
        boolean isCanDelete = false;
        String where = " 1=1 ";
        if (!TextUtils.isEmpty(audioId)) {
            where += " and id=" + audioId + "";
        }
        where += " order by create_time desc";
        try {
            List<AudioDB> dbs = PHDbUtils.getInstance().query(AudioDB.class, where);
            if (dbs == null || dbs.size() == 0) {
                isCanDelete = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isCanDelete;
    }

    /**
     * 读取所有【下载完成】的数据
     *
     * @return
     */
    public List<TasksManagerModel> getDownloadedTasks() {
        final List<TasksManagerModel> list = new ArrayList<>();
        String where = " 1=1 ";
        where += " order by create_time desc";
        try {
            List<AudioDB> dbs = PHDbUtils.getInstance().query(AudioDB.class, where);
            if (dbs == null || dbs.size() == 0) {
            } else {
                for (AudioDB audioDB : dbs) {
                    TasksManagerModel model = new TasksManagerModel();
                    model.setTaskid(audioDB.taskId);
                    model.setAlbumId(audioDB.getAlbum_id());
                    model.setAlbumName(audioDB.getAlbum_name());
                    model.setAudioId(audioDB.getId());
                    model.setName(audioDB.getName());
                    model.setUrl(audioDB.getUrl());
                    model.setAuthor(audioDB.getAuthor());
                    model.setPath(audioDB.getPath());
                    byte status = FileDownloader.getImpl().getStatus(audioDB.getTaskId(), audioDB.getPath());
                    if (status == FileDownloadStatus.completed) {
                        list.add(model);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 读取所有【下载完成】的数据
     *
     * @return
     */
    public List<Audio> getDownloadedAudios() {
        final List<Audio> list = new ArrayList<>();
        String where = " 1=1 ";
        where += " order by create_time desc";
        try {
            List<AudioDB> dbs = PHDbUtils.getInstance().query(AudioDB.class, where);
            if (dbs == null || dbs.size() == 0) {
            } else {
                for (AudioDB audioDB : dbs) {
                    Audio audio = new Audio(
                            audioDB.getId(),
                            audioDB.getAuthor(),
                            audioDB.getCover(),
                            audioDB.getDesc(),
                            true,
                            audioDB.getName(),
                            audioDB.getUrl()
                    );
                    audio.setAlbumId(audioDB.getAlbum_id());
                    audio.setAlbumName(audioDB.getAlbum_name());
                    audio.setAlbumAuthor(audioDB.getAuthor());
                    list.add(audio);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 读取所有【下载完成】的数据
     *
     * @return
     */
    public Audio getDownloadedAudio(String audioId) {
        String where = " 1=1 ";
        if (!TextUtils.isEmpty(audioId)) {
            where += " and id=" + audioId + "";
        }
        try {
            List<AudioDB> dbs = PHDbUtils.getInstance().query(AudioDB.class, where);
            if (dbs == null || dbs.size() == 0) {
            } else {
                AudioDB audioDB = dbs.get(0);
                Audio audio = new Audio(
                        audioDB.getId(),
                        audioDB.getAuthor(),
                        audioDB.getCover(),
                        audioDB.getDesc(),
                        true,
                        audioDB.getName(),
                        audioDB.getUrl()
                );
                audio.setAlbumId(TextUtils.isEmpty(audioDB.getAlbum_id()) ? "" : audioDB.getAlbum_id());
                audio.setAlbumName(TextUtils.isEmpty(audioDB.getAlbum_name()) ? "" : audioDB.getAlbum_name());
                audio.setAlbumAuthor(TextUtils.isEmpty(audioDB.getAuthor()) ? "" : audioDB.getAuthor());
                return audio;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 读取【正在下载】的数据
     *
     * @return
     */
    public List<TasksManagerModel> getDownloadingTasks() {
        final List<TasksManagerModel> list = new ArrayList<>();
        String where = " 1=1 ";
        try {
            List<AudioDB> dbs = PHDbUtils.getInstance().query(AudioDB.class, where);
            if (dbs == null || dbs.size() == 0) {
            } else {
                for (AudioDB audioDB : dbs) {
                    TasksManagerModel model = new TasksManagerModel();
                    model.setTaskid(audioDB.taskId);
                    model.setAlbumId(audioDB.getAlbum_id());
                    model.setAlbumName(audioDB.getAlbum_name());
                    model.setAudioId(audioDB.getId());
                    model.setName(audioDB.getName());
                    model.setUrl(audioDB.getUrl());
                    model.setAuthor(audioDB.getAuthor());
                    model.setPath(audioDB.getPath());
                    byte status = FileDownloader.getImpl().getStatus(audioDB.getTaskId(), audioDB.getPath());
                    model.setStatus(status);
                    if (status != FileDownloadStatus.completed) {
                        list.add(model);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


}
